package org.yjc.controller;



import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.JqgridParam;
import org.yjc.custom.JqgridUtils;
import org.yjc.entity.Student;
import org.yjc.entity.TbClass;
import org.yjc.form.StudentForm;
import org.yjc.form.TbClassForm;
import org.yjc.service.StudentService;
import org.yjc.service.TbClassService;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	@Autowired
	private StudentService studentservice;
	@Autowired
	private TbClassService tbclassservice;
	
	@RequestMapping(value="/studentmain")
	public String studentmain(ModelMap map) {
		map.put("student", studentservice.findAll());
		return "student/studentmain";
	}
	/***
	 * 查询所有班级传到前台
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/studentmanage")
	public String studentmanage(ModelMap map) {
		map.put("tbClass",tbclassservice.findAll());
		return "student/studentmanage";
	}
	/***
	 * 将班级id传到前台
	 * @param tbclassid
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/studentall")
	public String studentall(Integer tbclassid,ModelMap map) {
		map.put("tbclassid",tbclassid);
		return "student/studentall";
	}
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Object list(JqgridParam param,Integer tbclassid) {
		Pageable pageable = param.buildPageable();
		Page<Student> page = null;
		if(!param.getSearch()) {
			page=studentservice.findByTbClassId(pageable,tbclassid);
		}else {
			page=studentservice.findByTbClassId(param.buildSpec(), pageable,tbclassid);
		}
		HashMap<String, Object> result = JqgridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(String id,StudentForm form,Integer tbclassid) {
		String oper = form.getOper();
		if("del".equals(oper)) {
			studentservice.deleteById(Integer.parseInt(id));
		}else if("add".equals(oper)) {
			Student student = new Student();
			BeanUtils.copyProperties(form, student);
			TbClass tbclass = tbclassservice.findById(tbclassid);
			student.setTbClass(tbclass);
			student.setPassWord("1111");
			studentservice.save(student);
		}else if("edit".equals(oper)) {
			Student student = new Student();
			student.setId(Integer.parseInt(id));
			BeanUtils.copyProperties(form, student);
			TbClass tbclass = tbclassservice.findById(tbclassid);
			student.setTbClass(tbclass);
			student.setPassWord("1111");
			studentservice.save(student);
		}
		return "OK";
	}
	
	@RequestMapping(value="/score")
	public String chakan(HttpSession session,ModelMap map) {
		Student student = (Student) session.getAttribute("student");
		Integer id = student.getId();
		Student student2 = studentservice.findById(id);
		map.put("student2",student2);
		return "student/score";
	}
}
